//
//  DJDetailPageController.m
//  HB
//
//  Created by 陶秋安 on 16/3/14.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "DJDetailPageController.h"

#import "WXApi.h"

@implementation DJDetailPageController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadHtml];
}

- (void)actionShareSheet:(HJCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 1:
            [self doShareSession];
            break;
        case 2:
            [self doShareTimeline];
            break;
            
    }
}

// 底部弹出层 3.实现代理方法，需要遵守HJCActionSheetDelegate代理协议
- (void)actionSheet:(HJCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
    }
}

//设置导航栏按钮
-(void) setBackBarButton
{
    UIImage* image = [UIImage imageNamed:@"back"];
    
    UIBarButtonItem *item=[[UIBarButtonItem alloc] init];
    
    [item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [item setBackButtonTitlePositionAdjustment:UIOffsetMake(0.0, -400.0) forBarMetrics:UIBarMetricsDefault];
    
    self.navigationItem.backBarButtonItem = item;
}

- (void)setNavigationRight:(NSString *)imageName sel:(SEL)sel
{
    
    UIButton *btn=[self customButton:@"shareicon" selector:sel];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem = item;

}
- (void)viewDidAppear:(BOOL)animated
{

}

-(void) OpenMusic
{
    HJCActionSheet *sheet = [[HJCActionSheet alloc] initWithDelegateForShare:self CancelTitle:@"关闭"];
    
    [sheet show];
}

- (void)setNavigationTitleImage:(NSString *)imageName
{
    self.navigationItem.title = [self.dict objectForKey:@"Title"];
}


- (void)loadHtml
{
    //[self showIndicator:LoadingTip autoHide:NO afterDelay:NO];
    [self executeContentOp];
}

-(void)executeContentOp
{
    //operation=[[DJDetailOperation alloc]initWithDelegate:self];
    [self opSuccessForGet:self andFlag:1];
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation = nil;
    
    //NSMutableDictionary *info= [[NSMutableDictionary alloc]init];
    
    NSString *urlString = [[NSBundle mainBundle] pathForResource:@"content_template2" ofType:@"html"];
    
    NSString *htmlString = [self htmlConvert];
    
    [self.webViewDJDetail loadHTMLString:htmlString baseURL:[NSURL URLWithString:urlString]];
}

- (NSString *)htmlConvert
{
    NSString *file = [[NSBundle mainBundle] pathForResource:@"content_template" ofType:@"html"];
    
    NSString *html = [[NSString alloc] initWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    
    html = [html stringByReplacingOccurrencesOfString:@"{{title}}" withString:[self.dict objectForKey:@"PageTitle"]];
    
    html = [html stringByReplacingOccurrencesOfString:@"{{authorname}}" withString:[self.dict objectForKey:@"Title"]];
    
    html = [html stringByReplacingOccurrencesOfString:@"{{body}}" withString:[self.dict objectForKey:@"PageContent"]];
    
    ///html = [html stringByReplacingOccurrencesOfString:@"{{headimg}}" withString:[self.dict objectForKey:@"DJHeadImg"]];
    
    //    if (info.images.count > 0) {
    //        NSString *img = nil;
    //
    //        for (ContentImageInfo *imageInfo in info.images) {
    //            img = [NSString stringWithFormat:HtmlImage, imageInfo.src];
    //            html = [html stringByReplacingOccurrencesOfString:imageInfo.ref withString:img];
    //        }
    //    }
    
    return html;
}

- (void)doShareSession
{
    WXMediaMessage *message = [WXMediaMessage message];
    
    message.title = [self.dict objectForKey:@"Title"];
    
    message.description = [self.dict objectForKey:@"PageTitle"];

    [message setThumbImage:[UIImage imageNamed:@"res2.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    
    ext.webpageUrl = [self.dict objectForKey:@"ShareURL"];
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    
    req.message = message;
    
    req.bText = NO;
    
    req.scene = WXSceneSession;
    
    if (![WXApi sendReq:req]) {
        [UIView showAlertView:@"提示" andMessage:@"未安装微信" andHost:self];
    }
}

- (void)doShareTimeline
{
    WXMediaMessage *message = [WXMediaMessage message];
    
    message.title = [self.dict objectForKey:@"Title"];
    
    message.description = [self.dict objectForKey:@"PageTitle"];
    
    [message setThumbImage:[UIImage imageNamed:@"res2.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    
    ext.webpageUrl = [self.dict objectForKey:@"ShareURL"];
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    
    req.message = message;
    
    req.bText = NO;
    
    req.scene = WXSceneSession;
    
    if (![WXApi sendReq:req]) {
        [UIView showAlertView:@"提示" andMessage:@"未安装微信" andHost:self];
    }
}

@end
